package com.cgx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cgx.common.BaseContext;
import com.cgx.mapper.OrdersMapper;
import com.cgx.pojo.AddressBook;
import com.cgx.pojo.Orders;
import com.cgx.pojo.ShoppingCart;
import com.cgx.pojo.User;
import com.cgx.service.AddressBookService;
import com.cgx.service.OrdersService;
import com.cgx.service.ShoppingCartService;
import com.cgx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Override
    @Transactional
    public void submit(Orders orders) {
        //获得当前用户id
        Long userId = BaseContext.getCurrentId();
        //查询当前用户的购物车信息
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        if(list==null||list.size()==0){
            return;
        }
        //查询用户数据
        User user = userService.getById(userId);
        //查询地址数据
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressBookService.getById(addressBookId);
        //向订单插入数据
        long id = IdWorker.getId();//订单号
        orders.setNumber(String.valueOf(id));
        this.save(orders);
        //向订单明细表插入数据

        //下单完后清空购物车

    }
}
